package com.shensi.adminCollect.controller.integral;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.integral.IntegralWares;
import com.shensi.adminCollect.service.integral.IIntegralWaresService;
import com.shensi.adminCollect.service.system.ISysDeptService;
import com.shensi.adminCollect.service.system.ISysStationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 积分商品管理Controller
 *
 * @author qujia
 * @date 2021-03-01
 */
@RestController
@RequestMapping("/integral/wares")
public class IntegralWaresController extends BaseController
{
    @Autowired
    private IIntegralWaresService integralWaresService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysStationUserService stationUserService;
    /**
     * 查询积分商品管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                integralWares.setStationIdList(list);
                integralWares.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            integralWares.setStationId(currentUser.getUser().getStationId());
        }
        List<String> wareslist =new ArrayList<>();
        wareslist.add("1");
        wareslist.add("2");
        integralWares.setWaresStatusList(wareslist);
        startPage();
        List<IntegralWares> waresList = integralWaresService.selectIntegralWaresList(integralWares);
        return getDataTable(waresList);
    }

    /**
     * 导出积分商品管理列表
     */
    @GetMapping("/export")
    public AjaxResult export(IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();

        if("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                integralWares.setStationIdList(list);
                integralWares.setStationId(null);
            }
        }else if("2".equals(currentUser.getUser().getUserLevel())) {
            integralWares.setStationId(currentUser.getUser().getStationId());
        }
        integralWares.setWaresStatus("1");
        List<IntegralWares> waresList = integralWaresService.selectIntegralWaresList(integralWares);
        ExcelUtil<IntegralWares> util = new ExcelUtil<IntegralWares>(IntegralWares.class);
        return util.exportExcel(waresList, "wares");
    }

    /**
     * 获取积分商品管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(integralWaresService.selectIntegralWaresById(id));
    }

    /**
     * 新增积分商品管理
     */
    @PostMapping
    public AjaxResult add(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if("2".equals(currentUser.getUser().getUserLevel())){
            integralWares.setStationId(currentUser.getUser().getStationId());
        }
        if(integralWares.getStationId()==null){
            return AjaxResult.error("油站信息为空");
        }
        integralWares.setCreateBy(currentUser.getUser().getUserId().toString());
        integralWares.setCreateTime(DateUtils.getNowDate());
        integralWares.setWaresStatus("1");
        return toAjax(integralWaresService.insertIntegralWares(integralWares));
    }

    /**
     * 修改积分商品管理。默认删除再做新增
     */
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralWares.setUpdateBy(currentUser.getUser().getUserId().toString());
        integralWares.setUpdateTime(DateUtils.getNowDate());
        return toAjax(integralWaresService.updateIntegralWares(integralWares));
    }
    /**
     * 修改积分商品管理
     */
    @PostMapping(value ="/editInfoWares")
    //@ResponseBody
    public AjaxResult editInfoWares(@RequestBody IntegralWares integralWares)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralWares.setUpdateBy(currentUser.getUser().getUserId().toString());
        integralWares.setUpdateTime(DateUtils.getNowDate());
        return toAjax(integralWaresService.editInfoWares(integralWares));
    }


    /**
     * 删除积分商品管理
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralWaresService.deleteIntegralWaresByIds(ids));
    }
}
